package com.middlegroup.payadmin.controller.sys;

import cn.afterturn.easypoi.entity.vo.NormalExcelConstants;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import cn.afterturn.easypoi.view.PoiBaseView;
import com.github.pagehelper.PageInfo;
import com.middlegroup.common.annotation.AllLog;
import com.middlegroup.common.enums.ServiceType;
import com.middlegroup.common.exception.BusinessException;
import com.middlegroup.payadmin.mapper.InBillsMapper;
import com.middlegroup.payadmin.mapper.OutBillsMapper;
import com.middlegroup.payadmin.pojo.dto.BillsDto;
import com.middlegroup.payadmin.pojo.dto.DataGrid;
import com.middlegroup.payadmin.pojo.dto.ManagerDto;
import com.middlegroup.payadmin.pojo.dto.SubjectDto;
import com.middlegroup.payadmin.pojo.po.Bank;
import com.middlegroup.payadmin.pojo.po.Channels;
import com.middlegroup.payadmin.service.InBillsService;
import com.middlegroup.payadmin.service.OutBillsService;
import com.middlegroup.payadmin.service.RoleMerchantMapService;
import com.middlegroup.payadmin.util.SessionUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 *银行管理页面
 * @author pc
 * @date 2019/1/21
 */
@Controller
@RequestMapping("/sys/bills")
@Api(value = "订单管理", description = "订单管理", tags = "1.2")
public class BillsController {
    private static final Logger logger = LoggerFactory.getLogger(BillsController.class);
    @Resource
    private OutBillsService outBillsService;

    @Resource
    private InBillsService inBillsService;

    @Resource
    private RoleMerchantMapService roleMerchantMapService;

    @Resource
    private InBillsMapper inBillsMapper;

    @Resource
    private OutBillsMapper outBillsMapper;

    @ApiIgnore
    @GetMapping("/list")
    public String list() {
        return "sys/bills/list";
    }
    /**
     * @param grid 分页条件
     * @param dto 查询条件
     * @return
     */
    @ApiOperation(value = "带条件分页查询")
    @GetMapping(value = "/query")
    @ResponseBody
    public PageInfo<BillsDto> query(DataGrid grid,BillsDto dto) {
        logger.debug("进入query："+dto.getServiceType());
        if (null == dto.getServiceType()){
            dto.setServiceType(ServiceType.IN.name());
        }
        //获取用户角色
        ManagerDto managerDto = SessionUtil.getSession();
        if (null != managerDto){
            //根据roleCode获取改角色拥有的商户
            List<String> merchantCode = roleMerchantMapService.getMerchantCodeByRoleCode(managerDto.getRoleCode());
            dto.setMerchantCode(merchantCode);
        }
        if (CollectionUtils.isEmpty(dto.getMerchantCode())){
            throw new BusinessException("角色未配置商户!");
        }
        logger.debug("开始查询："+dto.getServiceType());
        if (StringUtils.equals(ServiceType.OUT.name(),dto.getServiceType())){
            //出款
            return this.outBillsService.listBills(grid,dto);
        }else {
            logger.debug("进入查询："+dto.getServiceType());
            //收款
            return this.inBillsService.listBills(grid,dto);
        }
    }


    /**
     * 采用网上大神的POI操作, 速度杠杠的：http://git.oschina.net/jueyue/easypoi<br>
     *
     * @param response 响应流
     * @throws Exception 异常信息
     */
    @RequestMapping(value = "/export")
    @ResponseBody
    @ApiOperation(value = "订单导出", hidden = true)
    @RequiresPermissions("sys:bills:export")
    @AllLog(module = "订单管理", methods = "订单导出", description = "导出了订单信息")
    public void export(ModelMap map,BillsDto billsDto, HttpServletRequest request, HttpServletResponse response) throws Exception {
        String title = "订单列表";
        String sheetName = "订单列表详情";
        String fileName = "明细";
        List<BillsDto> list = null;
        //获取用户角色
        ManagerDto managerDto = SessionUtil.getSession();
        if (null != managerDto){
            //根据roleCode获取改角色拥有的商户
            List<String> merchantCode = roleMerchantMapService.getMerchantCodeByRoleCode(managerDto.getRoleCode());
            billsDto.setMerchantCode(merchantCode);
        }
        if (StringUtils.equals(ServiceType.OUT.name(),billsDto.getServiceType())){
            //出款
            title += "_出款";
            fileName += "_出款";
            list = outBillsMapper.listBills(billsDto);
        }else  if (StringUtils.equals(ServiceType.IN.name(),billsDto.getServiceType())){
            //收款
            title += "_收款";
            fileName += "_收款";
            list = inBillsMapper.listBills(billsDto);
        }
        ExportParams params = new ExportParams(title, sheetName, ExcelType.XSSF);



        map.put(NormalExcelConstants.DATA_LIST, list);
        map.put(NormalExcelConstants.CLASS, BillsDto.class);
        map.put(NormalExcelConstants.PARAMS, params);
        map.put(NormalExcelConstants.FILE_NAME,fileName);
        PoiBaseView.render(map, request, response, NormalExcelConstants.EASYPOI_EXCEL_VIEW);
    }

}
